package com.example.demo.controller;

import com.example.demo.constant.RedisConstant;
import com.example.demo.dto.CertificationRequestDTO;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.entity.StudentCertification;
import com.example.demo.service.CertificationManageService;
import com.example.demo.service.MailService;
import com.example.demo.service.StudentAttributeService;
import com.example.demo.service.StudentPointsService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import com.example.demo.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api("学生认证管理接口")
@RequestMapping("certificationManage")
@RestController
@Slf4j
public class CertificationManageController {
    @Autowired
    private CertificationManageService service;

    @Autowired
    private StudentPointsService studentPointsService;

    @Autowired
    private MailService mailService;

    @Autowired
    private RedisUtil redisUtil;

    @ResponseBody
    @ApiOperation(value = "获取学生列表接口")
    @RequestMapping(value = "getCertificationList", method = {RequestMethod.POST})
    public Result<PageVo<StudentCertification>> getList(@RequestBody PageBo pageBo) {
        PageVo<StudentCertification> list = service.getCertificationList(pageBo);
        return Result.ok(list);
    }

    //按照学号檢索
    @ResponseBody
    @ApiOperation(value = "搜索日志列表接口")
    @RequestMapping(value = "search", method = {RequestMethod.POST})
    public Result<PageVo<StudentCertification>> getListSearch(@RequestBody CertificationRequestDTO request) {
        String studentNum=request.getStudentNumber();

        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<StudentCertification> logs = service.searchCertification(studentNum,pageBo);
        return Result.ok(logs);

    }

    @ResponseBody
    @ApiOperation("免冠照片显示接口")
    @RequestMapping(value = "findBareheadedPhoto/{studentNumber}")
    public Result<String> findBareheadedPhoto(@PathVariable("studentNumber")String studentNumber) {
        Result<String> message = new Result<>();
        if (studentNumber != null) {
            StudentCertification result = service.getByNumber(studentNumber);

            String bareheadedPhoto = result.getBareheadedPhoto();
            if(bareheadedPhoto != null){
                message.setData("https://seedling-classmate.oss-cn-beijing.aliyuncs.com/" + bareheadedPhoto);
                return message;
            }
            else{
                return null;
            }
        }
        return null;
    }

    @ResponseBody
    @ApiOperation("录取通知书显示接口")
    @RequestMapping(value = "findAdmissionNotice/{studentNumber}")
    public Result<String> findAdmissionNotice(@PathVariable("studentNumber")String studentNumber) {
        Result<String> message = new Result<>();
        if (studentNumber != null) {
            StudentCertification result = service.getByNumber(studentNumber);

            String admissionNotice = result.getAdmissionNotice();
            if(admissionNotice != null){
                message.setData("https://seedling-classmate.oss-cn-beijing.aliyuncs.com/" + admissionNotice);
                return message;
            }
            else{
                return null;
            }
        }
        return null;
    }

    @ResponseBody
    @ApiOperation("身份证人像面显示接口")
    @RequestMapping(value = "findIdCardFace/{studentNumber}")
    public Result<String> findIdCardFace(@PathVariable("studentNumber")String studentNumber) {
        Result<String> message = new Result<>();
        if (studentNumber != null) {
            StudentCertification result = service.getByNumber(studentNumber);

            String idCardFace = result.getIdCardFace();
            if(idCardFace != null){
                message.setData("https://seedling-classmate.oss-cn-beijing.aliyuncs.com/" + idCardFace);
                return message;
            }
            else{
                return null;
            }
        }
        return null;
    }

    @ResponseBody
    @ApiOperation("身份证国徽面显示接口")
    @RequestMapping(value = "findIdCardNational/{studentNumber}")
    public Result<String> findIdCardNational(@PathVariable("studentNumber")String studentNumber) {
        Result<String> message = new Result<>();
        if (studentNumber != null) {
            StudentCertification result = service.getByNumber(studentNumber);

            String idCardNational = result.getIdCardNational();
            if(idCardNational != null){
                message.setData("https://seedling-classmate.oss-cn-beijing.aliyuncs.com/" + idCardNational);
                return message;
            }
            else{
                return null;
            }

        }
        return null;
    }

    @ResponseBody
    @ApiOperation("审核通过接口")
    @RequestMapping(value = "approveRequest", method = {RequestMethod.POST})
    public Result<String> updateUserInfo(@RequestBody StudentCertification student) {
        String number=student.getStudentNumber();
        int id = service.getIdByNumber(number);
        String studentName=service.getStudentName(id);
        if(id!=0){
            service.approveStudent(student);
            mailService.sendStatusMail("审核通过", studentName, id,"");
            redisUtil.addOrUpdate(RedisConstant.Student_Points_Rank_Name, id, 0);
            return Result.ok("审核通过", "已审核通过");
        }
        else return Result.fail("审核失败");
    }

    @ResponseBody
    @ApiOperation("审核不通过接口")
    @RequestMapping(value = "disapproveRequest", method = {RequestMethod.POST})
    public Result<String> disapproveInfo(@RequestBody StudentCertification user) {
        int id = user.getId();
        //獲取用戶名
        int studentId = service.getIdByNumber(user.getStudentNumber());
        String studentName=service.getStudentName(studentId);
        //不同意原因
        int disapprove = user.getDisapproveReason();
        String disapproveReason;
        if(disapprove== 1) {
            disapproveReason="由于非本校录取通知书,";
        } else if(disapprove == 2) {
            disapproveReason="由于免冠照片与人像面不符,";
        } else if(disapprove == 3) {
            disapproveReason="由于录取通知书姓名与身份证姓名不符,";
        } else if(disapprove == 4) {
            disapproveReason="由于身份证过期,";
        } else if(disapprove == 5) {
            disapproveReason="由于录取通知书上的信息有误,";
        } else if(disapprove == 6) {
            disapproveReason="由于照片中存在遮挡物或修改痕迹,";
        } else if(disapprove == 7) {
            disapproveReason="由于录取通知书的清晰度不足,";
        } else {
            disapproveReason = "由于未知的拒绝原因,";
        }
        if(id!=0){
            service.disapproveStudent(id, disapprove);
            mailService.sendStatusMail("审核未通过", studentName, id,disapproveReason);
            return Result.ok("修改成功");
        }

        else return Result.fail("修改失败");
    }

}
